package com.myidis.servlet;

import com.auxiliary.SignalLampCalculator;
import com.auxiliary.SignalLampTimeSequence;
import com.auxiliary.TimeSequence;
import com.myidis.entity.*;
import com.myidis.mapper.*;
import com.myidis.request.SLCalculatorReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class SignalLampCalculatorServlet {
    @Autowired
    SignalQuotaManagementMapper signalQuotaManagementMapper;
    @Autowired
    SignalLampResultMapper signalResultMapper;
    @Autowired
    SignalThresholdMapper signalThresholdMapper;
    @Autowired
    QuotaMapper quotaMapper;
    @Autowired
    QuotaDataMapper quotaDataMapper;

    public ArrayList<SignalLampTimeSequence> calculate(SLCalculatorReq req) {
        ArrayList<SignalLampTimeSequence> signalLampTimeSequenceList =  new ArrayList<SignalLampTimeSequence>();
        ArrayList<SignalQuota> sq = signalQuotaManagementMapper.getByCombination(req.getCombination());
        for(int i = 0; i < sq.size(); i++){
            int a = Integer.parseInt(sq.get(i).getQuota());
            TimeSequence ts = new TimeSequence(quotaDataMapper.getByOne(a));
            String quota = quotaMapper.getOne(ts.getQuota()).getChineseName();
            SignalThreshold th = signalThresholdMapper.getByQuota(quota,req.getSubject());
            signalLampTimeSequenceList.add(new SignalLampCalculator().Calculate(ts,th));
        }

        String subject = req.getSubject();
        String startTime = req.getStartTime();
        String endTime = req.getEndTime();
        String springDay = req.getSpringDay();
        String combination = req.getCombination();
        String calculator = req.getCalculator();
        String calTime = req.getCalTime();
        System.out.println(subject+" "+combination);
        signalResultMapper.insertList(new IndexResultList(0,subject,springDay,calTime,combination,calculator,startTime,endTime));


        for(int i = 0; i < signalLampTimeSequenceList.size(); i++){
            for(int j = 0; j < signalLampTimeSequenceList.get(i).getValueList().size(); j++ ){
                String time = signalLampTimeSequenceList.get(0).getDateList().get(j);
                double lamp = signalLampTimeSequenceList.get(i).getValueList().get(j);
                FullSLResult DataSql = new FullSLResult(subject,time,lamp);
                signalResultMapper.insertDetail(DataSql);

            }



        }
        return signalLampTimeSequenceList;
    }

    public ArrayList<IndexResultList> all() {
        return signalResultMapper.list();
    }

    public ArrayList<FullSLResult> getData(IndexResultList sea) {
        return signalResultMapper.getData(sea);
    }

    public FullSLResult getInfo(int sea) {
        return signalResultMapper.getInfo(sea);
    }

    public int delect(int del) {
        signalResultMapper.deleteData(del);
        return signalResultMapper.delete(del);
    }
}
